package models

import (
	"github.com/gomodule/redigo/redis"
	"time"
)

type LeaveWord struct {
	Id           int64           `json:"id" orm:"pk;auto"`
	Name         string          `json:"name"`
	Email        string          `json:"email"`
	Photo        string          `json:"photo"`
	Homepage     string          `json:"homepage"`
	Word         string          `json:"word" orm:"type(text);size(1000)'"`
	Time         time.Time       `json:"time"`
	Pass         int8            `json:"pass"`
	IsAuthor     int8            `json:"is_author"`
	LeaveWordTwo []*LeaveWordTwo `json:"leave_word_two" orm:"reverse(many)"`
}

func (l *LeaveWord) TableName() string {
	return TableName("leave_word")
}

// AddDayNum 留言日增数量加一
func (l *LeaveWord) AddDayNum() (err error) {
	get := Conn()
	viewsInADay := "leaveWordNums:" + time.Unix(time.Now().Unix(), 0).String()[:10]
	_, err = get.Do("incr", viewsInADay)
	return
}

// GetAllNumsAWeek 一周新增量
func (l *LeaveWord) GetAllNumsAWeek() (n int, err error) {
	get := pool.Get()
	viewsInADayOne := "leaveWordNums:" + time.Unix(time.Now().Add(-7*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayTwo := "leaveWordNums:" + time.Unix(time.Now().Add(-6*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayThree := "leaveWordNums:" + time.Unix(time.Now().Add(-5*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFour := "leaveWordNums:" + time.Unix(time.Now().Add(-4*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFive := "leaveWordNums:" + time.Unix(time.Now().Add(-3*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySix := "leaveWordNums:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySenven := "leaveWordNums:" + time.Unix(time.Now().Add(-1*time.Hour*24).Unix(), 0).String()[:10]
	one, err := redis.Int(get.Do("Get", viewsInADayOne))
	if err != nil {
		one = 0
	}
	two, err := redis.Int(get.Do("Get", viewsInADayTwo))
	if err != nil {
		two = 0
	}
	three, err := redis.Int(get.Do("Get", viewsInADayThree))
	if err != nil {
		three = 0
	}
	four, err := redis.Int(get.Do("Get", viewsInADayFour))
	if err != nil {
		four = 0
	}
	five, err := redis.Int(get.Do("Get", viewsInADayFive))
	if err != nil {
		five = 0
	}
	six, err := redis.Int(get.Do("Get", viewsInADaySix))
	if err != nil {
		six = 0
	}
	senven, err := redis.Int(get.Do("Get", viewsInADaySenven))
	if err != nil {
		senven = 0
	}
	n = one + two + three + four + five + six + senven
	return
}

// GetEveryDayNumsInWeek 一周中每一天的留言的增量
func (l *LeaveWord) GetEveryDayNumsInWeek() (n []int, err error) {
	get := pool.Get()
	viewsInADayOne := "leaveWordNums:" + time.Unix(time.Now().Add(-7*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayTwo := "leaveWordNums:" + time.Unix(time.Now().Add(-6*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayThree := "leaveWordNums:" + time.Unix(time.Now().Add(-5*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFour := "leaveWordNums:" + time.Unix(time.Now().Add(-4*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADayFive := "leaveWordNums:" + time.Unix(time.Now().Add(-3*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySix := "leaveWordNums:" + time.Unix(time.Now().Add(-2*time.Hour*24).Unix(), 0).String()[:10]
	viewsInADaySenven := "leaveWordNums:" + time.Unix(time.Now().Add(-1*time.Hour*24).Unix(), 0).String()[:10]
	one, err := redis.Int(get.Do("Get", viewsInADayOne))
	if err != nil {
		one = 0
	}
	n = append(n, one)
	two, err := redis.Int(get.Do("Get", viewsInADayTwo))
	if err != nil {
		two = 0
	}
	n = append(n, two)
	three, err := redis.Int(get.Do("Get", viewsInADayThree))
	if err != nil {
		three = 0
	}
	n = append(n, three)
	four, err := redis.Int(get.Do("Get", viewsInADayFour))
	if err != nil {
		four = 0
	}
	n = append(n, four)
	five, err := redis.Int(get.Do("Get", viewsInADayFive))
	if err != nil {
		five = 0
	}
	n = append(n, five)
	six, err := redis.Int(get.Do("Get", viewsInADaySix))
	if err != nil {
		six = 0
	}
	n = append(n, six)
	senven, err := redis.Int(get.Do("Get", viewsInADaySenven))
	if err != nil {
		senven = 0
	}
	n = append(n, senven)
	return
}
